package Stack_04.BasedOnTheLinkedList_02;

/*
 * @Author 罗
 * @date 2020/3/13 - 8:27 下午
 *
 */

public class StackBasedOnTheLinkedList {
    private ObjectNode top;

    private ObjectNode<String> bottom;

    public void show(){
        ObjectNode temp = bottom.getNextNode();
        while(true){
            if(temp == null){
                break;
            }
            System.out.println(temp);
            temp = temp.getNextNode();
        }
    }


    public void push(ObjectNode objectNode){

        this.top.setNextNode(objectNode);

        if(isEmpty()){
            bottom.setNextNode(objectNode);
        }
        this.top = objectNode;
    }
    public ObjectNode pop(){
        if(isEmpty()){
            System.err.println("链表栈中数据为空");
            return null;
        }

        ObjectNode temp = bottom;
        while(true){
            if(temp.getNextNode() == top){
                break;
            }
            temp = temp.getNextNode();
        }
        ObjectNode result = temp.getNextNode();
        temp.setNextNode(null);
        this.top = temp;
        return result;
    }

    public boolean isEmpty(){
        return top == bottom;
    }


    public StackBasedOnTheLinkedList() {
        this.bottom = new ObjectNode<String>("栈底");
        this.top = bottom;

    }
}
